{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a796dc1b",
   "metadata": {},
   "source": [
    "文件处理是任何 Web 应用程序的重要组成部分。\n",
    "\n",
    "Python 具有多个用于创建、读取、更新和删除文件的函数。\n",
    "\n",
    "# 文件处理\n",
    "在 Python 中处理文件的关键函数是 open() 函数。\n",
    "\n",
    "open() 函数有两个参数； 文件名和模式。\n",
    "\n",
    "打开文件有四种不同的方法（模式）：\n",
    "\n",
    "“r” - 读取 - 默认值。 打开一个文件进行读取，如果文件不存在则报错\n",
    "\n",
    "\"a\" - Append - 打开一个文件进行追加，如果文件不存在则创建该文件\n",
    "\n",
    "\"w\" - 写入 - 打开一个文件进行写入，如果文件不存在则创建该文件\n",
    "\n",
    "\"x\" - Create - 创建指定的文件，如果文件存在则返回错误\n",
    "\n",
    "此外，您可以指定文件是否应作为二进制或文本模式处理\n",
    "\n",
    "“t” - 文本 - 默认值。 文本模式\n",
    "\n",
    "“b” - 二进制 - 二进制模式（例如图像）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2bef294d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "f = open(\"demofile.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9413c95d",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"demofile.txt\", \"rt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0492189",
   "metadata": {},
   "source": [
    "# 文件读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76084132",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %load demofile.txt\n",
    "Hello! Welcome to demofile.txt\n",
    "This file is for testing purposes.\n",
    "Good Luck!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0b41680e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello! Welcome to demofile.txt\n",
      "This file is for testing purposes.\n",
      "Good Luck!\n"
     ]
    }
   ],
   "source": [
    "f = open(\"demofile.txt\", \"r\")\n",
    "print(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a60f5d1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n"
     ]
    }
   ],
   "source": [
    "# 只读文件的一部分\n",
    "# 默认情况下， read() 方法返回整个文本，但也可以指定要返回的字符数：\n",
    "f = open(\"demofile.txt\", \"r\")\n",
    "print(f.read(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0767a23f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello! Welcome to demofile.txt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 读一行\n",
    "f = open(\"demofile.txt\", \"r\")\n",
    "print(f.readline())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "51098de3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello! Welcome to demofile.txt\n",
      "\n",
      "This file is for testing purposes.\n",
      "\n",
      "Good Luck!\n"
     ]
    }
   ],
   "source": [
    "f = open(\"demofile.txt\", \"r\")\n",
    "for x in f:\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c763263",
   "metadata": {},
   "source": [
    "# 关闭文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7cd4af74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello! Welcome to demofile.txt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f = open(\"demofile.txt\", \"r\")\n",
    "print(f.readline())\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57a9d598",
   "metadata": {},
   "source": [
    "# 写入现有文件\n",
    "要写入现有文件，您必须向 open() 函数添加一个参数：\n",
    "\n",
    "\"a\" - Append - 将追加到文件的末尾\n",
    "\n",
    "\"w\" - 写入 - 将覆盖任何现有内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eb9149d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Now the file has more content!\n"
     ]
    }
   ],
   "source": [
    "f = open(\"demofile2.txt\", \"a\")\n",
    "f.write(\"Now the file has more content!\")\n",
    "f.close()\n",
    "\n",
    "#open and read the file after the appending:\n",
    "f = open(\"demofile2.txt\", \"r\")\n",
    "print(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "965bf73c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Woops! I have deleted the content!\n"
     ]
    }
   ],
   "source": [
    "f = open(\"demofile3.txt\", \"w\")\n",
    "f.write(\"Woops! I have deleted the content!\")\n",
    "f.close()\n",
    "\n",
    "#open and read the file after the appending:\n",
    "f = open(\"demofile3.txt\", \"r\")\n",
    "print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15cc1230",
   "metadata": {},
   "source": [
    "# 创建新文件\n",
    "要在 Python 中创建新文件，请使用 open() 方法，并使用以下参数之一：\n",
    "\n",
    "\"x\" - 创建 - 将创建一个文件，如果文件存在则返回错误\n",
    "\n",
    "\"a\" - Append - 如果指定的文件不存在，将创建一个文件\n",
    "\n",
    "\"w\" - 写入 - 如果指定的文件不存在，将创建一个文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1728f83b",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"myfile.txt\", \"x\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b329a793",
   "metadata": {},
   "source": [
    "# 删除文件\n",
    "要删除文件，必须导入 OS 模块，并运行其 os.remove() 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "63eb339d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.remove(\"demofile.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22ca4f3a",
   "metadata": {},
   "source": [
    "# 检查文件是否存在\n",
    "为避免出现错误，您可能需要在尝试删除文件之前检查该文件是否存在："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2f641e1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The file does not exist\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "if os.path.exists(\"demofile.txt\"):\n",
    "  os.remove(\"demofile.txt\")\n",
    "else:\n",
    "  print(\"The file does not exist\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
